package com.sky.module.support.crypt;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * AES256加解密
 */
public class AESEncrypt {

    public static final String UTF_8 = "UTF-8";
    private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    static {
        try {
            //预热
            Cipher.getInstance(ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成 SecretKey
     *
     * @param secret
     * @param salt
     * @return
     */
    private static SecretKey generateSecretKey(String secret, String salt) {
        return new SecretKeySpec(secret.getBytes(), "AES");
    }

    /**
     * AES256加密
     *
     * @param content
     * @param secretKey
     * @return
     */
    private static byte[] encryptAES(byte[] content, SecretKey secretKey) {
        byte[] str = null;
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv));
            str = cipher.doFinal(content);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return str;
    }

    /**
     * AES256解密
     *
     * @param bytes
     * @param secretKey
     * @return
     */
    private static byte[] decryptAES(byte[] bytes, SecretKey secretKey) {
        byte[] decryptStr = null;
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
            decryptStr = cipher.doFinal(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return decryptStr;
    }

    /**
     * BASE64加密
     *
     * @param content
     * @return
     */
    public static String encrypt(String content, String key, String salt) throws UnsupportedEncodingException {
        SecretKey secretKey = generateSecretKey(key, salt);
        byte[] bytes = content.getBytes(UTF_8);
        return Base64.encodeBase64String(encryptAES(bytes, secretKey));
    }

    /**
     * BASE64解密
     *
     * @param content
     * @return
     */
    public static String decrypt(String content, String key, String salt) throws UnsupportedEncodingException {
        SecretKey secretKey = generateSecretKey(key, salt);
        return new String(decryptAES(Base64.decodeBase64(content), secretKey), UTF_8);
    }


    public static void main(String[] args) {
        try {
            //参数加密
            String context = "{\"password\":\"123456\",\"account\":\"zhouhz\"}";
            String s = encrypt(context, "wba2022041620221", "123456");
            System.out.println(s);

            //参数解密
            String s1 = decrypt("DiqP8oyUDPpNMK2eUaGVg62MXAgY7vjhyEZMa1Kzpgp0Sen53p/S+di1znmeJGCk", "wba2022041620221", "123456");
            System.out.println(s1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
